/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadSetorEmpresa.java
 *
 * Created on 18/10/2011, 14:18:50
 */
package visao;

import empresa.Empresa;
import fachada.FachadaBDR;
import java.awt.Color;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.SealedObject;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import setorEmpresa.SetorEmpresa;
import util.CamposObrigatoriosException;
import util.ErroAcessoRepositorioException;
import util.KeyValue;

/**
 *
 * @author Administrador
 */
public class CadSetorEmpresa extends javax.swing.JFrame {

    /** Creates new form CadSetorEmpresa */
    public CadSetorEmpresa() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/SetorEmpresa.gif")).getImage());
    }

    public CadSetorEmpresa(int idSetorEmpresa) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/SetorEmpresa.gif")).getImage());
        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            // obtendo a categoria
            SetorEmpresa objSetorEmpresa = objFachadaBDR.procurarSetorEmpresa(idSetorEmpresa);

            List<Empresa> empresa = objFachadaBDR.listarEmpresaAtivos();


            jTextField_idCodigo.setText(String.valueOf(objSetorEmpresa.getIdSetorEmpresa()));
            jTextField_CentroCusto.setText(objSetorEmpresa.getCentroCusto());
            jTextField_DescricaoSetor.setText(objSetorEmpresa.getDescricaoSetor());
            jCheckBox_Status.setSelected(objSetorEmpresa.getStatusSetorEmpresa());
            jComboBox_idEmpresa.setSelectedItem(new KeyValue(objSetorEmpresa.getEmpresa().getDescricaoEmpresa(), Integer.toString(objSetorEmpresa.getEmpresa().getIdEmpresa())));



        } catch (Exception ex) {

            jLabel_Menssagem.setText(ex.getMessage());
            jLabel_Menssagem.setForeground(Color.red);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jLabel_Codigo = new javax.swing.JLabel();
        jLabel_DescricaoSetor = new javax.swing.JLabel();
        jTextField_DescricaoSetor = new javax.swing.JTextField();
        jLabel_CentroCusto = new javax.swing.JLabel();
        jTextField_CentroCusto = new javax.swing.JTextField();
        jCheckBox_Status = new javax.swing.JCheckBox();
        jButton_Ok = new javax.swing.JButton();
        jButton_Cancelar = new javax.swing.JButton();
        jLabel_Menssagem = new javax.swing.JLabel();
        jTextField_idCodigo = new javax.swing.JTextField();
        jLabel_Empresa = new javax.swing.JLabel();
        jComboBox_idEmpresa = new javax.swing.JComboBox();

        jLabel3.setText("jLabel3");

        setTitle("Setor Empresa");
        setResizable(false);

        jLabel_Codigo.setText("Codigo :");

        jLabel_DescricaoSetor.setText("Nome :");

        jLabel_CentroCusto.setText("Centro de Custo :");

        jCheckBox_Status.setSelected(true);
        jCheckBox_Status.setText("Ativo");

        jButton_Ok.setText("OK");
        jButton_Ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_OkActionPerformed(evt);
            }
        });

        jButton_Cancelar.setText("Cancelar");
        jButton_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CancelarActionPerformed(evt);
            }
        });

        jTextField_idCodigo.setText("0");
        jTextField_idCodigo.setEnabled(false);

        jLabel_Empresa.setText("Empresa :");

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            List<Empresa> listEmpresa = objFachadaBDR.listarEmpresaAtivos();

            jComboBox_idEmpresa.addItem(new KeyValue("--- Favor Selecionar ---","0"));
            for (Empresa objEmpresa : listEmpresa) {
                jComboBox_idEmpresa.addItem(new KeyValue(objEmpresa.getNomeFantasia(), Integer.toString(objEmpresa.getIdEmpresa())));
            }

        } catch (Exception e) {
        }

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jButton_Ok, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton_Cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(225, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel_Menssagem, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
                .addGap(24, 24, 24))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_CentroCusto, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel_DescricaoSetor, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel_Codigo, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField_CentroCusto, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                            .addComponent(jTextField_DescricaoSetor, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(95, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField_idCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(166, 166, 166)
                        .addComponent(jCheckBox_Status)
                        .addGap(75, 75, 75))))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel_Empresa)
                .addGap(18, 18, 18)
                .addComponent(jComboBox_idEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel_Codigo)
                            .addComponent(jTextField_idCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(jCheckBox_Status)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_DescricaoSetor)
                    .addComponent(jTextField_DescricaoSetor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_CentroCusto)
                    .addComponent(jTextField_CentroCusto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Empresa)
                    .addComponent(jComboBox_idEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_Ok)
                    .addComponent(jButton_Cancelar))
                .addGap(18, 18, 18)
                .addComponent(jLabel_Menssagem, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_OkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_OkActionPerformed
        // TODO add your handling code here:
        String nome = jTextField_DescricaoSetor.getText();
        String centrocusto = jTextField_CentroCusto.getText();
        Boolean status = jCheckBox_Status.isSelected();
        int idSetorempresa = Integer.parseInt(jTextField_idCodigo.getText());
        int idSetorEmpresa = Integer.parseInt(((KeyValue) jComboBox_idEmpresa.getSelectedItem()).getValue());
        try {

            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            Empresa empresa = (Empresa) objFachadaBDR.procurarEmpresa(idSetorEmpresa);
            SetorEmpresa setorEmpresa = new SetorEmpresa(idSetorempresa, empresa, nome, centrocusto, rootPaneCheckingEnabled, null);

            if (idSetorempresa == 0) {
                objFachadaBDR.cadastrarSetorEmpresa(setorEmpresa);


                jTextField_CentroCusto.setText("");
                jTextField_DescricaoSetor.setText("");
                jCheckBox_Status.setSelected(false);
                jComboBox_idEmpresa.setSelectedIndex(0);

            } else {
                objFachadaBDR.atualizarSetorEmpresa(setorEmpresa);
            }

            String acao = (idSetorempresa == 0) ? "Cadastrada" : "Editada";
            jLabel_Menssagem.setText("Setor Empresa " + acao + " Com Sucesso!!!");
            jLabel_Menssagem.setForeground(Color.BLACK);

        } catch (Exception ex) {
            jLabel_Menssagem.setText(ex.getMessage());
            jLabel_Menssagem.setForeground(Color.red);
        }
    }//GEN-LAST:event_jButton_OkActionPerformed

    private void jButton_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelarActionPerformed
        // TODO add your handling code here:
        int cancelar = JOptionPane.showConfirmDialog(null, "Deseja cancelar ?");
        if (cancelar == JOptionPane.YES_OPTION) {
            dispose();

        }
    }//GEN-LAST:event_jButton_CancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CadSetorEmpresa().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Cancelar;
    private javax.swing.JButton jButton_Ok;
    private javax.swing.JCheckBox jCheckBox_Status;
    private javax.swing.JComboBox jComboBox_idEmpresa;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel_CentroCusto;
    private javax.swing.JLabel jLabel_Codigo;
    private javax.swing.JLabel jLabel_DescricaoSetor;
    private javax.swing.JLabel jLabel_Empresa;
    private javax.swing.JLabel jLabel_Menssagem;
    private javax.swing.JTextField jTextField_CentroCusto;
    private javax.swing.JTextField jTextField_DescricaoSetor;
    private javax.swing.JTextField jTextField_idCodigo;
    // End of variables declaration//GEN-END:variables
}
